package com.example.controller.weixin;

import com.example.annotation.PassToken;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.api.impl.WxMpServiceHttpClientImpl;
import me.chanjar.weixin.mp.bean.result.WxMpQrCodeTicket;
import me.chanjar.weixin.mp.config.impl.WxMpDefaultConfigImpl;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


/**
 * @author ChenXiaoBin
 * on 2019-12-06
 */
@Slf4j
@RestController
@RequestMapping("/login/code")
public class WxMpQrcodeController {

    @GetMapping("")
    @ApiOperation(value = "获取二维码", notes = "获取二维码")
    @PassToken
    public WxMpQrCodeTicket code() throws WxErrorException {
        //相当与注册
        WxMpService mpService = new WxMpServiceHttpClientImpl();
        WxMpDefaultConfigImpl wxMpConfigStorage = new WxMpDefaultConfigImpl();
        wxMpConfigStorage.setAppId("wx65253d4d6da76947");
        wxMpConfigStorage.setToken("cxbcxb");
        wxMpConfigStorage.setAesKey("12");
        wxMpConfigStorage.setSecret("eaac587494f79f0692a597ef6452693c");
        mpService.setWxMpConfigStorage(wxMpConfigStorage);
        mpService.addConfigStorage(wxMpConfigStorage.getAppId(), wxMpConfigStorage);
        //换取临时二维码ticket
        WxMpQrCodeTicket wxMpQrCodeTicket = mpService.getQrcodeService().qrCodeCreateTmpTicket("QR_SCENE_STR", 10 * 60);
        return wxMpQrCodeTicket;
    }

}
